3 * @defgroup Database Database
5 * This file deals with database interface functions
6 * and query specifics/optimisations.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 use Psr\Log\LoggerAwareInterface
;
27 use Psr\Log\LoggerInterface
;
30 * Database abstraction object
33 abstract class DatabaseBase
implements IDatabase
, LoggerAwareInterface
{
34 /** Number of times to re-try an operation in case of deadlock */
35 const DEADLOCK_TRIES
= 4;
36 /** Minimum time to wait before retry, in microseconds */
37 const DEADLOCK_DELAY_MIN
= 500000;
38 /** Maximum time to wait before retry */
39 const DEADLOCK_DELAY_MAX
= 1500000;
41 /** How long before it is worth doing a dummy query to test the connection */
43 const PING_QUERY
= 'SELECT 1 AS ping';
45 const TINY_WRITE_SEC
= .010;
46 const SLOW_WRITE_SEC
= .500;
47 const SMALL_WRITE_ROWS
= 100;
49 /** @var string SQL query */
50 protected $mLastQuery = '';
52 protected $mDoneWrites = false;
53 /** @var string|bool */
54 protected $mPHPError = false;
66 /** @var BagOStuff APC cache */
68 /** @var LoggerInterface */
69 protected $connLogger;
70 /** @var LoggerInterface */
71 protected $queryLogger;
72 /** @var callback Error logging callback */
73 protected $errorLogger;
75 /** @var resource Database connection */
76 protected $mConn = null;
78 protected $mOpened = false;
80 /** @var array[] List of (callable, method name) */
81 protected $mTrxIdleCallbacks = [];
82 /** @var array[] List of (callable, method name) */
83 protected $mTrxPreCommitCallbacks = [];
84 /** @var array[] List of (callable, method name) */
85 protected $mTrxEndCallbacks = [];
86 /** @var array[] Map of (name => (callable, method name)) */
87 protected $mTrxRecurringCallbacks = [];
88 /** @var bool Whether to suppress triggering of transaction end callbacks */
89 protected $mTrxEndCallbacksSuppressed = false;
92 protected $mTablePrefix;
100 protected $mLBInfo = [];
101 /** @var bool|null */
102 protected $mDefaultBigSelects = null;
103 /** @var array|bool */
104 protected $mSchemaVars = false;
106 protected $mSessionVars = [];
107 /** @var array|null */
108 protected $preparedArgs;
109 /** @var string|bool|null Stashed value of html_errors INI setting */
110 protected $htmlErrors;
112 protected $delimiter = ';';
115 * Either 1 if a transaction is active or 0 otherwise.
116 * The other Trx fields may not be meaningfull if this is 0.
120 protected $mTrxLevel = 0;
122 * Either a short hexidecimal string if a transaction is active or ""
125 * @see DatabaseBase::mTrxLevel
127 protected $mTrxShortId = '';
129 * The UNIX time that the transaction started. Callers can assume that if
130 * snapshot isolation is used, then the data is *at least* up to date to that
131 * point (possibly more up-to-date since the first SELECT defines the snapshot).
134 * @see DatabaseBase::mTrxLevel
136 private $mTrxTimestamp = null;
137 /** @var float Lag estimate at the time of BEGIN */
138 private $mTrxReplicaLag = null;
140 * Remembers the function name given for starting the most recent transaction via begin().
141 * Used to provide additional context for error reporting.
144 * @see DatabaseBase::mTrxLevel
146 private $mTrxFname = null;
148 * Record if possible write queries were done in the last transaction started
151 * @see DatabaseBase::mTrxLevel
153 private $mTrxDoneWrites = false;
155 * Record if the current transaction was started implicitly due to DBO_TRX being set.
158 * @see DatabaseBase::mTrxLevel
160 private $mTrxAutomatic = false;
162 * Array of levels of atomicity within transactions
166 private $mTrxAtomicLevels = [];
168 * Record if the current transaction was started implicitly by DatabaseBase::startAtomic
172 private $mTrxAutomaticAtomic = false;
174 * Track the write query callers of the current transaction
178 private $mTrxWriteCallers = [];
180 * @var float Seconds spent in write queries for the current transaction
182 private $mTrxWriteDuration = 0.0;
184 * @var integer Number of write queries for the current transaction
186 private $mTrxWriteQueryCount = 0;
188 * @var float Like mTrxWriteQueryCount but excludes lock-bound, easy to replicate, queries
190 private $mTrxWriteAdjDuration = 0.0;
192 * @var integer Number of write queries counted in mTrxWriteAdjDuration
194 private $mTrxWriteAdjQueryCount = 0;
196 * @var float RTT time estimate
198 private $mRTTEstimate = 0.0;
200 /** @var array Map of (name => 1) for locks obtained via lock() */
201 private $mNamedLocksHeld = [];
203 /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
204 private $lazyMasterHandle;
208 * @var resource File handle for upgrade
210 protected $fileHandle = null;
214 * @var string[] Process cache of VIEWs names in the database
216 protected $allViews = null;
218 /** @var float UNIX timestamp */
219 protected $lastPing = 0.0;
221 /** @var int[] Prior mFlags values */
222 private $priorFlags = [];
226 /** @var TransactionProfiler */
227 protected $trxProfiler;
232 * FIXME: It is possible to construct a Database object with no associated
233 * connection object, by specifying no parameters to __construct(). This
234 * feature is deprecated and should be removed.
236 * IDatabase classes should not be constructed directly in external
237 * code. DatabaseBase::factory() should be used instead.
239 * @param array $params Parameters passed from DatabaseBase::factory()
241 function __construct( array $params ) {
242 global $wgDBprefix, $wgDBmwschema;
244 $this->srvCache
= ObjectCache
::getLocalServerInstance( 'hash' );
246 $server = $params['host'];
247 $user = $params['user'];
248 $password = $params['password'];
249 $dbName = $params['dbname'];
250 $flags = $params['flags'];
251 $tablePrefix = $params['tablePrefix'];
252 $schema = $params['schema'];
253 $foreign = $params['foreign'];
255 $this->cliMode
= isset( $params['cliMode'] )
257 : ( PHP_SAPI
=== 'cli' );
259 $this->mFlags
= $flags;
260 if ( $this->mFlags
& DBO_DEFAULT
) {
261 if ( $this->cliMode
) {
262 $this->mFlags
&= ~DBO_TRX
;
264 $this->mFlags |
= DBO_TRX
;
268 $this->mSessionVars
= $params['variables'];
270 /** Get the default table prefix*/
271 if ( $tablePrefix === 'get from global' ) {
272 $this->mTablePrefix
= $wgDBprefix;
274 $this->mTablePrefix
= $tablePrefix;
277 /** Get the database schema*/
278 if ( $schema === 'get from global' ) {
279 $this->mSchema
= $wgDBmwschema;
281 $this->mSchema
= $schema;
284 $this->mForeign
= $foreign;
286 $this->profiler
= isset( $params['profiler'] )
287 ?
$params['profiler']
288 : Profiler
::instance(); // @TODO: remove global state
289 $this->trxProfiler
= isset( $params['trxProfiler'] )
290 ?
$params['trxProfiler']
291 : new TransactionProfiler();
292 $this->connLogger
= isset( $params['connLogger'] )
293 ?
$params['connLogger']
294 : new \Psr\Log\
NullLogger();
295 $this->queryLogger
= isset( $params['queryLogger'] )
296 ?
$params['queryLogger']
297 : new \Psr\Log\
NullLogger();
300 $this->open( $server, $user, $password, $dbName );
305 * Given a DB type, construct the name of the appropriate child class of
306 * IDatabase. This is designed to replace all of the manual stuff like:
307 * $class = 'Database' . ucfirst( strtolower( $dbType ) );
308 * as well as validate against the canonical list of DB types we have
310 * This factory function is mostly useful for when you need to connect to a
311 * database other than the MediaWiki default (such as for external auth,
312 * an extension, et cetera). Do not use this to connect to the MediaWiki
313 * database. Example uses in core:
314 * @see LoadBalancer::reallyOpenConnection()
315 * @see ForeignDBRepo::getMasterDB()
316 * @see WebInstallerDBConnect::execute()
320 * @param string $dbType A possible DB type
321 * @param array $p An array of options to pass to the constructor.
322 * Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver
323 * @return IDatabase|null If the database driver or extension cannot be found
324 * @throws InvalidArgumentException If the database driver or extension cannot be found
326 final public static function factory( $dbType, $p = [] ) {
327 global $wgCommandLineMode;
329 $canonicalDBTypes = [
330 'mysql' => [ 'mysqli', 'mysql' ],
338 $dbType = strtolower( $dbType );
339 if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
340 $possibleDrivers = $canonicalDBTypes[$dbType];
341 if ( !empty( $p['driver'] ) ) {
342 if ( in_array( $p['driver'], $possibleDrivers ) ) {
343 $driver = $p['driver'];
345 throw new InvalidArgumentException( __METHOD__
.
346 " type '$dbType' does not support driver '{$p['driver']}'" );
349 foreach ( $possibleDrivers as $posDriver ) {
350 if ( extension_loaded( $posDriver ) ) {
351 $driver = $posDriver;
359 if ( $driver === false ) {
360 throw new InvalidArgumentException( __METHOD__
.
361 " no viable database extension found for type '$dbType'" );
364 // Determine schema defaults. Currently Microsoft SQL Server uses $wgDBmwschema,
365 // and everything else doesn't use a schema (e.g. null)
366 // Although postgres and oracle support schemas, we don't use them (yet)
367 // to maintain backwards compatibility
369 'mssql' => 'get from global',
372 $class = 'Database' . ucfirst( $driver );
373 if ( class_exists( $class ) && is_subclass_of( $class, 'IDatabase' ) ) {
374 // Resolve some defaults for b/c
375 $p['host'] = isset( $p['host'] ) ?
$p['host'] : false;
376 $p['user'] = isset( $p['user'] ) ?
$p['user'] : false;
377 $p['password'] = isset( $p['password'] ) ?
$p['password'] : false;
378 $p['dbname'] = isset( $p['dbname'] ) ?
$p['dbname'] : false;
379 $p['flags'] = isset( $p['flags'] ) ?
$p['flags'] : 0;
380 $p['variables'] = isset( $p['variables'] ) ?
$p['variables'] : [];
381 $p['tablePrefix'] = isset( $p['tablePrefix'] ) ?
$p['tablePrefix'] : 'get from global';
382 if ( !isset( $p['schema'] ) ) {
383 $p['schema'] = isset( $defaultSchemas[$dbType] ) ?
$defaultSchemas[$dbType] : null;
385 $p['foreign'] = isset( $p['foreign'] ) ?
$p['foreign'] : false;
386 $p['cliMode'] = $wgCommandLineMode;
388 $conn = new $class( $p );
389 if ( isset( $p['connLogger'] ) ) {
390 $conn->connLogger
= $p['connLogger'];
392 if ( isset( $p['queryLogger'] ) ) {
393 $conn->queryLogger
= $p['queryLogger'];
395 if ( isset( $p['errorLogger'] ) ) {
396 $conn->errorLogger
= $p['errorLogger'];
398 $conn->errorLogger
= [ MWExceptionHandler
::class, 'logException' ];
407 public function setLogger( LoggerInterface
$logger ) {
408 $this->queryLogger
= $logger;
411 public function getServerInfo() {
412 return $this->getServerVersion();
416 * @return string Command delimiter used by this database engine
418 public function getDelimiter() {
419 return $this->delimiter
;
423 * Boolean, controls output of large amounts of debug information.
424 * @param bool|null $debug
425 * - true to enable debugging
426 * - false to disable debugging
427 * - omitted or null to do nothing
429 * @return bool|null Previous value of the flag
431 public function debug( $debug = null ) {
432 return wfSetBit( $this->mFlags
, DBO_DEBUG
, $debug );
435 public function bufferResults( $buffer = null ) {
436 if ( is_null( $buffer ) ) {
437 return !(bool)( $this->mFlags
& DBO_NOBUFFER
);
439 return !wfSetBit( $this->mFlags
, DBO_NOBUFFER
, !$buffer );
444 * Turns on (false) or off (true) the automatic generation and sending
445 * of a "we're sorry, but there has been a database error" page on
446 * database errors. Default is on (false). When turned off, the
447 * code should use lastErrno() and lastError() to handle the
448 * situation as appropriate.
450 * Do not use this function outside of the Database classes.
452 * @param null|bool $ignoreErrors
453 * @return bool The previous value of the flag.
455 protected function ignoreErrors( $ignoreErrors = null ) {
456 return wfSetBit( $this->mFlags
, DBO_IGNORE
, $ignoreErrors );
459 public function trxLevel() {
460 return $this->mTrxLevel
;
463 public function trxTimestamp() {
464 return $this->mTrxLevel ?
$this->mTrxTimestamp
: null;
467 public function tablePrefix( $prefix = null ) {
468 return wfSetVar( $this->mTablePrefix
, $prefix );
471 public function dbSchema( $schema = null ) {
472 return wfSetVar( $this->mSchema
, $schema );
476 * Set the filehandle to copy write statements to.
478 * @param resource $fh File handle
480 public function setFileHandle( $fh ) {
481 $this->fileHandle
= $fh;
484 public function getLBInfo( $name = null ) {
485 if ( is_null( $name ) ) {
486 return $this->mLBInfo
;
488 if ( array_key_exists( $name, $this->mLBInfo
) ) {
489 return $this->mLBInfo
[$name];
496 public function setLBInfo( $name, $value = null ) {
497 if ( is_null( $value ) ) {
498 $this->mLBInfo
= $name;
500 $this->mLBInfo
[$name] = $value;
504 public function setLazyMasterHandle( IDatabase
$conn ) {
505 $this->lazyMasterHandle
= $conn;
509 * @return IDatabase|null
510 * @see setLazyMasterHandle()
513 public function getLazyMasterHandle() {
514 return $this->lazyMasterHandle
;
518 * @param TransactionProfiler $profiler
521 public function setTransactionProfiler( TransactionProfiler
$profiler ) {
522 $this->trxProfiler
= $profiler;
526 * Returns true if this database supports (and uses) cascading deletes
530 public function cascadingDeletes() {
535 * Returns true if this database supports (and uses) triggers (e.g. on the page table)
539 public function cleanupTriggers() {
544 * Returns true if this database is strict about what can be put into an IP field.
545 * Specifically, it uses a NULL value instead of an empty string.
549 public function strictIPs() {
554 * Returns true if this database uses timestamps rather than integers
558 public function realTimestamps() {
562 public function implicitGroupby() {
566 public function implicitOrderby() {
571 * Returns true if this database can do a native search on IP columns
572 * e.g. this works as expected: .. WHERE rc_ip = '127.42.12.102/32';
576 public function searchableIPs() {
581 * Returns true if this database can use functional indexes
585 public function functionalIndexes() {
589 public function lastQuery() {
590 return $this->mLastQuery
;
593 public function doneWrites() {
594 return (bool)$this->mDoneWrites
;
597 public function lastDoneWrites() {
598 return $this->mDoneWrites ?
: false;
601 public function writesPending() {
602 return $this->mTrxLevel
&& $this->mTrxDoneWrites
;
605 public function writesOrCallbacksPending() {
606 return $this->mTrxLevel
&& (
607 $this->mTrxDoneWrites ||
$this->mTrxIdleCallbacks ||
$this->mTrxPreCommitCallbacks
611 public function pendingWriteQueryDuration( $type = self
::ESTIMATE_TOTAL
) {
612 if ( !$this->mTrxLevel
) {
614 } elseif ( !$this->mTrxDoneWrites
) {
619 case self
::ESTIMATE_DB_APPLY
:
621 $rttAdjTotal = $this->mTrxWriteAdjQueryCount
* $rtt;
622 $applyTime = max( $this->mTrxWriteAdjDuration
- $rttAdjTotal, 0 );
623 // For omitted queries, make them count as something at least
624 $omitted = $this->mTrxWriteQueryCount
- $this->mTrxWriteAdjQueryCount
;
625 $applyTime +
= self
::TINY_WRITE_SEC
* $omitted;
628 default: // everything
629 return $this->mTrxWriteDuration
;
633 public function pendingWriteCallers() {
634 return $this->mTrxLevel ?
$this->mTrxWriteCallers
: [];
637 public function isOpen() {
638 return $this->mOpened
;
641 public function setFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
642 if ( $remember === self
::REMEMBER_PRIOR
) {
643 array_push( $this->priorFlags
, $this->mFlags
);
645 $this->mFlags |
= $flag;
648 public function clearFlag( $flag, $remember = self
::REMEMBER_NOTHING
) {
649 if ( $remember === self
::REMEMBER_PRIOR
) {
650 array_push( $this->priorFlags
, $this->mFlags
);
652 $this->mFlags
&= ~
$flag;
655 public function restoreFlags( $state = self
::RESTORE_PRIOR
) {
656 if ( !$this->priorFlags
) {
660 if ( $state === self
::RESTORE_INITIAL
) {
661 $this->mFlags
= reset( $this->priorFlags
);
662 $this->priorFlags
= [];
664 $this->mFlags
= array_pop( $this->priorFlags
);
668 public function getFlag( $flag ) {
669 return !!( $this->mFlags
& $flag );
672 public function getProperty( $name ) {
676 public function getWikiID() {
677 if ( $this->mTablePrefix
) {
678 return "{$this->mDBname}-{$this->mTablePrefix}";
680 return $this->mDBname
;
685 * Return a path to the DBMS-specific SQL file if it exists,
686 * otherwise default SQL file
688 * @param string $filename
691 private function getSqlFilePath( $filename ) {
693 $dbmsSpecificFilePath = "$IP/maintenance/" . $this->getType() . "/$filename";
694 if ( file_exists( $dbmsSpecificFilePath ) ) {
695 return $dbmsSpecificFilePath;
697 return "$IP/maintenance/$filename";
702 * Return a path to the DBMS-specific schema file,
703 * otherwise default to tables.sql
707 public function getSchemaPath() {
708 return $this->getSqlFilePath( 'tables.sql' );
712 * Return a path to the DBMS-specific update key file,
713 * otherwise default to update-keys.sql
717 public function getUpdateKeysPath() {
718 return $this->getSqlFilePath( 'update-keys.sql' );
722 * Get information about an index into an object
723 * @param string $table Table name
724 * @param string $index Index name
725 * @param string $fname Calling function name
726 * @return mixed Database-specific index description class or false if the index does not exist
728 abstract function indexInfo( $table, $index, $fname = __METHOD__
);
731 * Wrapper for addslashes()
733 * @param string $s String to be slashed.
734 * @return string Slashed string.
736 abstract function strencode( $s );
739 * Called by serialize. Throw an exception when DB connection is serialized.
740 * This causes problems on some database engines because the connection is
741 * not restored on unserialize.
743 public function __sleep() {
744 throw new RuntimeException( 'Database serialization may cause problems, since ' .
745 'the connection is not restored on wakeup.' );
748 protected function installErrorHandler() {
749 $this->mPHPError
= false;
750 $this->htmlErrors
= ini_set( 'html_errors', '0' );
751 set_error_handler( [ $this, 'connectionerrorLogger' ] );
755 * @return bool|string
757 protected function restoreErrorHandler() {
758 restore_error_handler();
759 if ( $this->htmlErrors
!== false ) {
760 ini_set( 'html_errors', $this->htmlErrors
);
762 if ( $this->mPHPError
) {
763 $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError
);
764 $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
774 * @param string $errstr
776 public function connectionerrorLogger( $errno, $errstr ) {
777 $this->mPHPError
= $errstr;
781 * Create a log context to pass to PSR logging functions.
783 * @param array $extras Additional data to add to context
786 protected function getLogContext( array $extras = [] ) {
789 'db_server' => $this->mServer
,
790 'db_name' => $this->mDBname
,
791 'db_user' => $this->mUser
,
797 public function close() {
798 if ( $this->mConn
) {
799 if ( $this->trxLevel() ) {
800 $this->commit( __METHOD__
, self
::FLUSHING_INTERNAL
);
803 $closed = $this->closeConnection();
804 $this->mConn
= false;
805 } elseif ( $this->mTrxIdleCallbacks ||
$this->mTrxEndCallbacks
) { // sanity
806 throw new RuntimeException( "Transaction callbacks still pending." );
810 $this->mOpened
= false;
816 * Make sure isOpen() returns true as a sanity check
818 * @throws DBUnexpectedError
820 protected function assertOpen() {
821 if ( !$this->isOpen() ) {
822 throw new DBUnexpectedError( $this, "DB connection was already closed." );
827 * Closes underlying database connection
829 * @return bool Whether connection was closed successfully
831 abstract protected function closeConnection();
833 function reportConnectionError( $error = 'Unknown error' ) {
834 $myError = $this->lastError();
840 throw new DBConnectionError( $this, $error );
844 * The DBMS-dependent part of query()
846 * @param string $sql SQL query.
847 * @return ResultWrapper|bool Result object to feed to fetchObject,
848 * fetchRow, ...; or false on failure
850 abstract protected function doQuery( $sql );
853 * Determine whether a query writes to the DB.
854 * Should return true if unsure.
859 protected function isWriteQuery( $sql ) {
861 '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SET|SHOW|EXPLAIN|\(SELECT)\b/i', $sql );
866 * @return string|null
868 protected function getQueryVerb( $sql ) {
869 return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ?
strtoupper( $m[1] ) : null;
873 * Determine whether a SQL statement is sensitive to isolation level.
874 * A SQL statement is considered transactable if its result could vary
875 * depending on the transaction isolation level. Operational commands
876 * such as 'SET' and 'SHOW' are not considered to be transactable.
881 protected function isTransactableQuery( $sql ) {
882 $verb = $this->getQueryVerb( $sql );
883 return !in_array( $verb, [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET' ], true );
886 public function query( $sql, $fname = __METHOD__
, $tempIgnore = false ) {
889 $priorWritesPending = $this->writesOrCallbacksPending();
890 $this->mLastQuery
= $sql;
892 $isWrite = $this->isWriteQuery( $sql );
894 $reason = $this->getReadOnlyReason();
895 if ( $reason !== false ) {
896 throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
898 # Set a flag indicating that writes have been done
899 $this->mDoneWrites
= microtime( true );
902 # Add a comment for easy SHOW PROCESSLIST interpretation
903 if ( is_object( $wgUser ) && $wgUser->isItemLoaded( 'name' ) ) {
904 $userName = $wgUser->getName();
905 if ( mb_strlen( $userName ) > 15 ) {
906 $userName = mb_substr( $userName, 0, 15 ) . '...';
908 $userName = str_replace( '/', '', $userName );
913 // Add trace comment to the begin of the sql string, right after the operator.
914 // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (bug 42598)
915 $commentedSql = preg_replace( '/\s|$/', " /* $fname $userName */ ", $sql, 1 );
917 # Start implicit transactions that wrap the request if DBO_TRX is enabled
918 if ( !$this->mTrxLevel
&& $this->getFlag( DBO_TRX
)
919 && $this->isTransactableQuery( $sql )
921 $this->begin( __METHOD__
. " ($fname)", self
::TRANSACTION_INTERNAL
);
922 $this->mTrxAutomatic
= true;
925 # Keep track of whether the transaction has write queries pending
926 if ( $this->mTrxLevel
&& !$this->mTrxDoneWrites
&& $isWrite ) {
927 $this->mTrxDoneWrites
= true;
928 $this->trxProfiler
->transactionWritingIn(
929 $this->mServer
, $this->mDBname
, $this->mTrxShortId
);
932 if ( $this->debug() ) {
933 $this->queryLogger
->debug( "{$this->mDBname} {$commentedSql}" );
936 # Avoid fatals if close() was called
939 # Send the query to the server
940 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
942 # Try reconnecting if the connection was lost
943 if ( false === $ret && $this->wasErrorReissuable() ) {
944 $recoverable = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
945 # Stash the last error values before anything might clear them
946 $lastError = $this->lastError();
947 $lastErrno = $this->lastErrno();
948 # Update state tracking to reflect transaction loss due to disconnection
949 $this->handleTransactionLoss();
950 if ( $this->reconnect() ) {
951 $msg = __METHOD__
. ": lost connection to {$this->getServer()}; reconnected";
952 $this->connLogger
->warning( $msg );
953 $this->queryLogger
->warning(
954 "$msg:\n" . ( new RuntimeException() )->getTraceAsString() );
956 if ( !$recoverable ) {
957 # Callers may catch the exception and continue to use the DB
958 $this->reportQueryError( $lastError, $lastErrno, $sql, $fname );
960 # Should be safe to silently retry the query
961 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
964 $msg = __METHOD__
. ": lost connection to {$this->getServer()} permanently";
965 $this->connLogger
->error( $msg );
969 if ( false === $ret ) {
970 # Deadlocks cause the entire transaction to abort, not just the statement.
971 # http://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
972 # https://www.postgresql.org/docs/9.1/static/explicit-locking.html
973 if ( $this->wasDeadlock() ) {
974 if ( $this->explicitTrxActive() ||
$priorWritesPending ) {
975 $tempIgnore = false; // not recoverable
977 # Update state tracking to reflect transaction loss
978 $this->handleTransactionLoss();
981 $this->reportQueryError(
982 $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
985 $res = $this->resultObject( $ret );
990 private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) {
991 $isMaster = !is_null( $this->getLBInfo( 'master' ) );
992 # generalizeSQL() will probably cut down the query to reasonable
993 # logging size most of the time. The substr is really just a sanity check.
995 $queryProf = 'query-m: ' . substr( self
::generalizeSQL( $sql ), 0, 255 );
997 $queryProf = 'query: ' . substr( self
::generalizeSQL( $sql ), 0, 255 );
1000 # Include query transaction state
1001 $queryProf .= $this->mTrxShortId ?
" [TRX#{$this->mTrxShortId}]" : "";
1003 $startTime = microtime( true );
1004 $this->profiler
->profileIn( $queryProf );
1005 $ret = $this->doQuery( $commentedSql );
1006 $this->profiler
->profileOut( $queryProf );
1007 $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
1009 unset( $queryProfSection ); // profile out (if set)
1011 if ( $ret !== false ) {
1012 $this->lastPing
= $startTime;
1013 if ( $isWrite && $this->mTrxLevel
) {
1014 $this->updateTrxWriteQueryTime( $sql, $queryRuntime );
1015 $this->mTrxWriteCallers
[] = $fname;
1019 if ( $sql === self
::PING_QUERY
) {
1020 $this->mRTTEstimate
= $queryRuntime;
1023 $this->trxProfiler
->recordQueryCompletion(
1024 $queryProf, $startTime, $isWrite, $this->affectedRows()
1026 MWDebug
::query( $sql, $fname, $isMaster, $queryRuntime );
1032 * Update the estimated run-time of a query, not counting large row lock times
1034 * LoadBalancer can be set to rollback transactions that will create huge replication
1035 * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple
1036 * queries, like inserting a row can take a long time due to row locking. This method
1037 * uses some simple heuristics to discount those cases.
1039 * @param string $sql A SQL write query
1040 * @param float $runtime Total runtime, including RTT
1042 private function updateTrxWriteQueryTime( $sql, $runtime ) {
1043 // Whether this is indicative of replica DB runtime (except for RBR or ws_repl)
1044 $indicativeOfReplicaRuntime = true;
1045 if ( $runtime > self
::SLOW_WRITE_SEC
) {
1046 $verb = $this->getQueryVerb( $sql );
1047 // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
1048 if ( $verb === 'INSERT' ) {
1049 $indicativeOfReplicaRuntime = $this->affectedRows() > self
::SMALL_WRITE_ROWS
;
1050 } elseif ( $verb === 'REPLACE' ) {
1051 $indicativeOfReplicaRuntime = $this->affectedRows() > self
::SMALL_WRITE_ROWS
/ 2;
1055 $this->mTrxWriteDuration +
= $runtime;
1056 $this->mTrxWriteQueryCount +
= 1;
1057 if ( $indicativeOfReplicaRuntime ) {
1058 $this->mTrxWriteAdjDuration +
= $runtime;
1059 $this->mTrxWriteAdjQueryCount +
= 1;
1063 private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
1064 # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
1065 # Dropped connections also mean that named locks are automatically released.
1066 # Only allow error suppression in autocommit mode or when the lost transaction
1067 # didn't matter anyway (aside from DBO_TRX snapshot loss).
1068 if ( $this->mNamedLocksHeld
) {
1069 return false; // possible critical section violation
1070 } elseif ( $sql === 'COMMIT' ) {
1071 return !$priorWritesPending; // nothing written anyway? (T127428)
1072 } elseif ( $sql === 'ROLLBACK' ) {
1073 return true; // transaction lost...which is also what was requested :)
1074 } elseif ( $this->explicitTrxActive() ) {
1075 return false; // don't drop atomocity
1076 } elseif ( $priorWritesPending ) {
1077 return false; // prior writes lost from implicit transaction
1083 private function handleTransactionLoss() {
1084 $this->mTrxLevel
= 0;
1085 $this->mTrxIdleCallbacks
= []; // bug 65263
1086 $this->mTrxPreCommitCallbacks
= []; // bug 65263
1088 // Handle callbacks in mTrxEndCallbacks
1089 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_ROLLBACK
);
1090 $this->runTransactionListenerCallbacks( self
::TRIGGER_ROLLBACK
);
1092 } catch ( Exception
$e ) {
1093 // Already logged; move on...
1098 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
1099 if ( $this->ignoreErrors() ||
$tempIgnore ) {
1100 $this->queryLogger
->debug( "SQL ERROR (ignored): $error\n" );
1102 $sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
1103 $this->queryLogger
->error(
1104 "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
1105 $this->getLogContext( [
1106 'method' => __METHOD__
,
1109 'sql1line' => $sql1line,
1113 $this->queryLogger
->debug( "SQL ERROR: " . $error . "\n" );
1114 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
1119 * Intended to be compatible with the PEAR::DB wrapper functions.
1120 * http://pear.php.net/manual/en/package.database.db.intro-execute.php
1122 * ? = scalar value, quoted as necessary
1123 * ! = raw SQL bit (a function for instance)
1124 * & = filename; reads the file and inserts as a blob
1125 * (we don't use this though...)
1127 * @param string $sql
1128 * @param string $func
1132 protected function prepare( $sql, $func = __METHOD__
) {
1133 /* MySQL doesn't support prepared statements (yet), so just
1134 * pack up the query for reference. We'll manually replace
1137 return [ 'query' => $sql, 'func' => $func ];
1141 * Free a prepared query, generated by prepare().
1142 * @param string $prepared
1144 protected function freePrepared( $prepared ) {
1145 /* No-op by default */
1149 * Execute a prepared query with the various arguments
1150 * @param string $prepared The prepared sql
1151 * @param mixed $args Either an array here, or put scalars as varargs
1153 * @return ResultWrapper
1155 public function execute( $prepared, $args = null ) {
1156 if ( !is_array( $args ) ) {
1158 $args = func_get_args();
1159 array_shift( $args );
1162 $sql = $this->fillPrepared( $prepared['query'], $args );
1164 return $this->query( $sql, $prepared['func'] );
1168 * For faking prepared SQL statements on DBs that don't support it directly.
1170 * @param string $preparedQuery A 'preparable' SQL statement
1171 * @param array $args Array of Arguments to fill it with
1172 * @return string Executable SQL
1174 public function fillPrepared( $preparedQuery, $args ) {
1176 $this->preparedArgs
=& $args;
1178 return preg_replace_callback( '/(\\\\[?!&]|[?!&])/',
1179 [ &$this, 'fillPreparedArg' ], $preparedQuery );
1183 * preg_callback func for fillPrepared()
1184 * The arguments should be in $this->preparedArgs and must not be touched
1185 * while we're doing this.
1187 * @param array $matches
1188 * @throws DBUnexpectedError
1191 protected function fillPreparedArg( $matches ) {
1192 switch ( $matches[1] ) {
1201 list( /* $n */, $arg ) = each( $this->preparedArgs
);
1203 switch ( $matches[1] ) {
1205 return $this->addQuotes( $arg );
1209 # return $this->addQuotes( file_get_contents( $arg ) );
1210 throw new DBUnexpectedError(
1212 '& mode is not implemented. If it\'s really needed, uncomment the line above.'
1215 throw new DBUnexpectedError(
1217 'Received invalid match. This should never happen!'
1222 public function freeResult( $res ) {
1225 public function selectField(
1226 $table, $var, $cond = '', $fname = __METHOD__
, $options = []
1228 if ( $var === '*' ) { // sanity
1229 throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
1232 if ( !is_array( $options ) ) {
1233 $options = [ $options ];
1236 $options['LIMIT'] = 1;
1238 $res = $this->select( $table, $var, $cond, $fname, $options );
1239 if ( $res === false ||
!$this->numRows( $res ) ) {
1243 $row = $this->fetchRow( $res );
1245 if ( $row !== false ) {
1246 return reset( $row );
1252 public function selectFieldValues(
1253 $table, $var, $cond = '', $fname = __METHOD__
, $options = [], $join_conds = []
1255 if ( $var === '*' ) { // sanity
1256 throw new DBUnexpectedError( $this, "Cannot use a * field" );
1257 } elseif ( !is_string( $var ) ) { // sanity
1258 throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
1261 if ( !is_array( $options ) ) {
1262 $options = [ $options ];
1265 $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
1266 if ( $res === false ) {
1271 foreach ( $res as $row ) {
1272 $values[] = $row->$var;
1279 * Returns an optional USE INDEX clause to go after the table, and a
1280 * string to go at the end of the query.
1282 * @param array $options Associative array of options to be turned into
1283 * an SQL query, valid keys are listed in the function.
1285 * @see DatabaseBase::select()
1287 public function makeSelectOptions( $options ) {
1288 $preLimitTail = $postLimitTail = '';
1293 foreach ( $options as $key => $option ) {
1294 if ( is_numeric( $key ) ) {
1295 $noKeyOptions[$option] = true;
1299 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1301 $preLimitTail .= $this->makeOrderBy( $options );
1303 // if (isset($options['LIMIT'])) {
1304 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
1305 // isset($options['OFFSET']) ? $options['OFFSET']
1309 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1310 $postLimitTail .= ' FOR UPDATE';
1313 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1314 $postLimitTail .= ' LOCK IN SHARE MODE';
1317 if ( isset( $noKeyOptions['DISTINCT'] ) ||
isset( $noKeyOptions['DISTINCTROW'] ) ) {
1318 $startOpts .= 'DISTINCT';
1321 # Various MySQL extensions
1322 if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
1323 $startOpts .= ' /*! STRAIGHT_JOIN */';
1326 if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) {
1327 $startOpts .= ' HIGH_PRIORITY';
1330 if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
1331 $startOpts .= ' SQL_BIG_RESULT';
1334 if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
1335 $startOpts .= ' SQL_BUFFER_RESULT';
1338 if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
1339 $startOpts .= ' SQL_SMALL_RESULT';
1342 if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
1343 $startOpts .= ' SQL_CALC_FOUND_ROWS';
1346 if ( isset( $noKeyOptions['SQL_CACHE'] ) ) {
1347 $startOpts .= ' SQL_CACHE';
1350 if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) {
1351 $startOpts .= ' SQL_NO_CACHE';
1354 if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
1355 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1359 if ( isset( $options['IGNORE INDEX'] ) && is_string( $options['IGNORE INDEX'] ) ) {
1360 $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
1365 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1369 * Returns an optional GROUP BY with an optional HAVING
1371 * @param array $options Associative array of options
1373 * @see DatabaseBase::select()
1376 public function makeGroupByWithHaving( $options ) {
1378 if ( isset( $options['GROUP BY'] ) ) {
1379 $gb = is_array( $options['GROUP BY'] )
1380 ?
implode( ',', $options['GROUP BY'] )
1381 : $options['GROUP BY'];
1382 $sql .= ' GROUP BY ' . $gb;
1384 if ( isset( $options['HAVING'] ) ) {
1385 $having = is_array( $options['HAVING'] )
1386 ?
$this->makeList( $options['HAVING'], LIST_AND
)
1387 : $options['HAVING'];
1388 $sql .= ' HAVING ' . $having;
1395 * Returns an optional ORDER BY
1397 * @param array $options Associative array of options
1399 * @see DatabaseBase::select()
1402 public function makeOrderBy( $options ) {
1403 if ( isset( $options['ORDER BY'] ) ) {
1404 $ob = is_array( $options['ORDER BY'] )
1405 ?
implode( ',', $options['ORDER BY'] )
1406 : $options['ORDER BY'];
1408 return ' ORDER BY ' . $ob;
1414 // See IDatabase::select for the docs for this function
1415 public function select( $table, $vars, $conds = '', $fname = __METHOD__
,
1416 $options = [], $join_conds = [] ) {
1417 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
1419 return $this->query( $sql, $fname );
1422 public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__
,
1423 $options = [], $join_conds = []
1425 if ( is_array( $vars ) ) {
1426 $vars = implode( ',', $this->fieldNamesWithAlias( $vars ) );
1429 $options = (array)$options;
1430 $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
1431 ?
$options['USE INDEX']
1433 $ignoreIndexes = ( isset( $options['IGNORE INDEX'] ) && is_array( $options['IGNORE INDEX'] ) )
1434 ?
$options['IGNORE INDEX']
1437 if ( is_array( $table ) ) {
1439 $this->tableNamesWithIndexClauseOrJOIN( $table, $useIndexes, $ignoreIndexes, $join_conds );
1440 } elseif ( $table != '' ) {
1441 if ( $table[0] == ' ' ) {
1442 $from = ' FROM ' . $table;
1445 $this->tableNamesWithIndexClauseOrJOIN( [ $table ], $useIndexes, $ignoreIndexes, [] );
1451 list( $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ) =
1452 $this->makeSelectOptions( $options );
1454 if ( !empty( $conds ) ) {
1455 if ( is_array( $conds ) ) {
1456 $conds = $this->makeList( $conds, LIST_AND
);
1458 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex WHERE $conds $preLimitTail";
1460 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex $preLimitTail";
1463 if ( isset( $options['LIMIT'] ) ) {
1464 $sql = $this->limitResult( $sql, $options['LIMIT'],
1465 isset( $options['OFFSET'] ) ?
$options['OFFSET'] : false );
1467 $sql = "$sql $postLimitTail";
1469 if ( isset( $options['EXPLAIN'] ) ) {
1470 $sql = 'EXPLAIN ' . $sql;
1476 public function selectRow( $table, $vars, $conds, $fname = __METHOD__
,
1477 $options = [], $join_conds = []
1479 $options = (array)$options;
1480 $options['LIMIT'] = 1;
1481 $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
1483 if ( $res === false ) {
1487 if ( !$this->numRows( $res ) ) {
1491 $obj = $this->fetchObject( $res );
1496 public function estimateRowCount(
1497 $table, $vars = '*', $conds = '', $fname = __METHOD__
, $options = []
1500 $res = $this->select( $table, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options );
1503 $row = $this->fetchRow( $res );
1504 $rows = ( isset( $row['rowcount'] ) ) ?
(int)$row['rowcount'] : 0;
1510 public function selectRowCount(
1511 $tables, $vars = '*', $conds = '', $fname = __METHOD__
, $options = [], $join_conds = []
1514 $sql = $this->selectSQLText( $tables, '1', $conds, $fname, $options, $join_conds );
1515 $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count", $fname );
1518 $row = $this->fetchRow( $res );
1519 $rows = ( isset( $row['rowcount'] ) ) ?
(int)$row['rowcount'] : 0;
1526 * Removes most variables from an SQL query and replaces them with X or N for numbers.
1527 * It's only slightly flawed. Don't use for anything important.
1529 * @param string $sql A SQL Query
1533 protected static function generalizeSQL( $sql ) {
1534 # This does the same as the regexp below would do, but in such a way
1535 # as to avoid crashing php on some large strings.
1536 # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
1538 $sql = str_replace( "\\\\", '', $sql );
1539 $sql = str_replace( "\\'", '', $sql );
1540 $sql = str_replace( "\\\"", '', $sql );
1541 $sql = preg_replace( "/'.*'/s", "'X'", $sql );
1542 $sql = preg_replace( '/".*"/s', "'X'", $sql );
1544 # All newlines, tabs, etc replaced by single space
1545 $sql = preg_replace( '/\s+/', ' ', $sql );
1548 # except the ones surrounded by characters, e.g. l10n
1549 $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
1550 $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
1555 public function fieldExists( $table, $field, $fname = __METHOD__
) {
1556 $info = $this->fieldInfo( $table, $field );
1561 public function indexExists( $table, $index, $fname = __METHOD__
) {
1562 if ( !$this->tableExists( $table ) ) {
1566 $info = $this->indexInfo( $table, $index, $fname );
1567 if ( is_null( $info ) ) {
1570 return $info !== false;
1574 public function tableExists( $table, $fname = __METHOD__
) {
1575 $table = $this->tableName( $table );
1576 $old = $this->ignoreErrors( true );
1577 $res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname );
1578 $this->ignoreErrors( $old );
1583 public function indexUnique( $table, $index ) {
1584 $indexInfo = $this->indexInfo( $table, $index );
1586 if ( !$indexInfo ) {
1590 return !$indexInfo[0]->Non_unique
;
1594 * Helper for DatabaseBase::insert().
1596 * @param array $options
1599 protected function makeInsertOptions( $options ) {
1600 return implode( ' ', $options );
1603 public function insert( $table, $a, $fname = __METHOD__
, $options = [] ) {
1604 # No rows to insert, easy just return now
1605 if ( !count( $a ) ) {
1609 $table = $this->tableName( $table );
1611 if ( !is_array( $options ) ) {
1612 $options = [ $options ];
1616 if ( isset( $options['fileHandle'] ) ) {
1617 $fh = $options['fileHandle'];
1619 $options = $this->makeInsertOptions( $options );
1621 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
1623 $keys = array_keys( $a[0] );
1626 $keys = array_keys( $a );
1629 $sql = 'INSERT ' . $options .
1630 " INTO $table (" . implode( ',', $keys ) . ') VALUES ';
1634 foreach ( $a as $row ) {
1640 $sql .= '(' . $this->makeList( $row ) . ')';
1643 $sql .= '(' . $this->makeList( $a ) . ')';
1646 if ( $fh !== null && false === fwrite( $fh, $sql ) ) {
1648 } elseif ( $fh !== null ) {
1652 return (bool)$this->query( $sql, $fname );
1656 * Make UPDATE options array for DatabaseBase::makeUpdateOptions
1658 * @param array $options
1661 protected function makeUpdateOptionsArray( $options ) {
1662 if ( !is_array( $options ) ) {
1663 $options = [ $options ];
1668 if ( in_array( 'LOW_PRIORITY', $options ) ) {
1669 $opts[] = $this->lowPriorityOption();
1672 if ( in_array( 'IGNORE', $options ) ) {
1680 * Make UPDATE options for the DatabaseBase::update function
1682 * @param array $options The options passed to DatabaseBase::update
1685 protected function makeUpdateOptions( $options ) {
1686 $opts = $this->makeUpdateOptionsArray( $options );
1688 return implode( ' ', $opts );
1691 function update( $table, $values, $conds, $fname = __METHOD__
, $options = [] ) {
1692 $table = $this->tableName( $table );
1693 $opts = $this->makeUpdateOptions( $options );
1694 $sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET
);
1696 if ( $conds !== [] && $conds !== '*' ) {
1697 $sql .= " WHERE " . $this->makeList( $conds, LIST_AND
);
1700 return $this->query( $sql, $fname );
1703 public function makeList( $a, $mode = LIST_COMMA
) {
1704 if ( !is_array( $a ) ) {
1705 throw new DBUnexpectedError( $this, __METHOD__
. ' called with incorrect parameters' );
1711 foreach ( $a as $field => $value ) {
1713 if ( $mode == LIST_AND
) {
1715 } elseif ( $mode == LIST_OR
) {
1724 if ( ( $mode == LIST_AND ||
$mode == LIST_OR
) && is_numeric( $field ) ) {
1725 $list .= "($value)";
1726 } elseif ( ( $mode == LIST_SET
) && is_numeric( $field ) ) {
1728 } elseif ( ( $mode == LIST_AND ||
$mode == LIST_OR
) && is_array( $value ) ) {
1729 // Remove null from array to be handled separately if found
1730 $includeNull = false;
1731 foreach ( array_keys( $value, null, true ) as $nullKey ) {
1732 $includeNull = true;
1733 unset( $value[$nullKey] );
1735 if ( count( $value ) == 0 && !$includeNull ) {
1736 throw new InvalidArgumentException( __METHOD__
. ": empty input for field $field" );
1737 } elseif ( count( $value ) == 0 ) {
1738 // only check if $field is null
1739 $list .= "$field IS NULL";
1741 // IN clause contains at least one valid element
1742 if ( $includeNull ) {
1743 // Group subconditions to ensure correct precedence
1746 if ( count( $value ) == 1 ) {
1747 // Special-case single values, as IN isn't terribly efficient
1748 // Don't necessarily assume the single key is 0; we don't
1749 // enforce linear numeric ordering on other arrays here.
1750 $value = array_values( $value )[0];
1751 $list .= $field . " = " . $this->addQuotes( $value );
1753 $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
1755 // if null present in array, append IS NULL
1756 if ( $includeNull ) {
1757 $list .= " OR $field IS NULL)";
1760 } elseif ( $value === null ) {
1761 if ( $mode == LIST_AND ||
$mode == LIST_OR
) {
1762 $list .= "$field IS ";
1763 } elseif ( $mode == LIST_SET
) {
1764 $list .= "$field = ";
1768 if ( $mode == LIST_AND ||
$mode == LIST_OR ||
$mode == LIST_SET
) {
1769 $list .= "$field = ";
1771 $list .= $mode == LIST_NAMES ?
$value : $this->addQuotes( $value );
1778 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
1781 foreach ( $data as $base => $sub ) {
1782 if ( count( $sub ) ) {
1783 $conds[] = $this->makeList(
1784 [ $baseKey => $base, $subKey => array_keys( $sub ) ],
1790 return $this->makeList( $conds, LIST_OR
);
1792 // Nothing to search for...
1798 * Return aggregated value alias
1800 * @param array $valuedata
1801 * @param string $valuename
1805 public function aggregateValue( $valuedata, $valuename = 'value' ) {
1809 public function bitNot( $field ) {
1813 public function bitAnd( $fieldLeft, $fieldRight ) {
1814 return "($fieldLeft & $fieldRight)";
1817 public function bitOr( $fieldLeft, $fieldRight ) {
1818 return "($fieldLeft | $fieldRight)";
1821 public function buildConcat( $stringList ) {
1822 return 'CONCAT(' . implode( ',', $stringList ) . ')';
1825 public function buildGroupConcatField(
1826 $delim, $table, $field, $conds = '', $join_conds = []
1828 $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
1830 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1834 * @param string $field Field or column to cast
1838 public function buildStringCast( $field ) {
1842 public function selectDB( $db ) {
1843 # Stub. Shouldn't cause serious problems if it's not overridden, but
1844 # if your database engine supports a concept similar to MySQL's
1845 # databases you may as well.
1846 $this->mDBname
= $db;
1851 public function getDBname() {
1852 return $this->mDBname
;
1855 public function getServer() {
1856 return $this->mServer
;
1860 * Format a table name ready for use in constructing an SQL query
1862 * This does two important things: it quotes the table names to clean them up,
1863 * and it adds a table prefix if only given a table name with no quotes.
1865 * All functions of this object which require a table name call this function
1866 * themselves. Pass the canonical name to such functions. This is only needed
1867 * when calling query() directly.
1869 * @note This function does not sanitize user input. It is not safe to use
1870 * this function to escape user input.
1871 * @param string $name Database table name
1872 * @param string $format One of:
1873 * quoted - Automatically pass the table name through addIdentifierQuotes()
1874 * so that it can be used in a query.
1875 * raw - Do not add identifier quotes to the table name
1876 * @return string Full database name
1878 public function tableName( $name, $format = 'quoted' ) {
1879 global $wgSharedDB, $wgSharedPrefix, $wgSharedTables, $wgSharedSchema;
1880 # Skip the entire process when we have a string quoted on both ends.
1881 # Note that we check the end so that we will still quote any use of
1882 # use of `database`.table. But won't break things if someone wants
1883 # to query a database table with a dot in the name.
1884 if ( $this->isQuotedIdentifier( $name ) ) {
1888 # Lets test for any bits of text that should never show up in a table
1889 # name. Basically anything like JOIN or ON which are actually part of
1890 # SQL queries, but may end up inside of the table value to combine
1891 # sql. Such as how the API is doing.
1892 # Note that we use a whitespace test rather than a \b test to avoid
1893 # any remote case where a word like on may be inside of a table name
1894 # surrounded by symbols which may be considered word breaks.
1895 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
1899 # Split database and table into proper variables.
1900 # We reverse the explode so that database.table and table both output
1901 # the correct table.
1902 $dbDetails = explode( '.', $name, 3 );
1903 if ( count( $dbDetails ) == 3 ) {
1904 list( $database, $schema, $table ) = $dbDetails;
1905 # We don't want any prefix added in this case
1907 } elseif ( count( $dbDetails ) == 2 ) {
1908 list( $database, $table ) = $dbDetails;
1909 # We don't want any prefix added in this case
1910 # In dbs that support it, $database may actually be the schema
1911 # but that doesn't affect any of the functionality here
1915 list( $table ) = $dbDetails;
1916 if ( $wgSharedDB !== null # We have a shared database
1917 && $this->mForeign
== false # We're not working on a foreign database
1918 && !$this->isQuotedIdentifier( $table ) # Prevent shared tables listing '`table`'
1919 && in_array( $table, $wgSharedTables ) # A shared table is selected
1921 $database = $wgSharedDB;
1922 $schema = $wgSharedSchema === null ?
$this->mSchema
: $wgSharedSchema;
1923 $prefix = $wgSharedPrefix === null ?
$this->mTablePrefix
: $wgSharedPrefix;
1926 $schema = $this->mSchema
; # Default schema
1927 $prefix = $this->mTablePrefix
; # Default prefix
1931 # Quote $table and apply the prefix if not quoted.
1932 # $tableName might be empty if this is called from Database::replaceVars()
1933 $tableName = "{$prefix}{$table}";
1934 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $tableName ) && $tableName !== '' ) {
1935 $tableName = $this->addIdentifierQuotes( $tableName );
1938 # Quote $schema and merge it with the table name if needed
1939 if ( strlen( $schema ) ) {
1940 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $schema ) ) {
1941 $schema = $this->addIdentifierQuotes( $schema );
1943 $tableName = $schema . '.' . $tableName;
1946 # Quote $database and merge it with the table name if needed
1947 if ( $database !== null ) {
1948 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
1949 $database = $this->addIdentifierQuotes( $database );
1951 $tableName = $database . '.' . $tableName;
1958 * Fetch a number of table names into an array
1959 * This is handy when you need to construct SQL for joins
1962 * extract( $dbr->tableNames( 'user', 'watchlist' ) );
1963 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1964 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1968 public function tableNames() {
1969 $inArray = func_get_args();
1972 foreach ( $inArray as $name ) {
1973 $retVal[$name] = $this->tableName( $name );
1980 * Fetch a number of table names into an zero-indexed numerical array
1981 * This is handy when you need to construct SQL for joins
1984 * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' );
1985 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1986 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1990 public function tableNamesN() {
1991 $inArray = func_get_args();
1994 foreach ( $inArray as $name ) {
1995 $retVal[] = $this->tableName( $name );
2002 * Get an aliased table name
2003 * e.g. tableName AS newTableName
2005 * @param string $name Table name, see tableName()
2006 * @param string|bool $alias Alias (optional)
2007 * @return string SQL name for aliased table. Will not alias a table to its own name
2009 public function tableNameWithAlias( $name, $alias = false ) {
2010 if ( !$alias ||
$alias == $name ) {
2011 return $this->tableName( $name );
2013 return $this->tableName( $name ) . ' ' . $this->addIdentifierQuotes( $alias );
2018 * Gets an array of aliased table names
2020 * @param array $tables [ [alias] => table ]
2021 * @return string[] See tableNameWithAlias()
2023 public function tableNamesWithAlias( $tables ) {
2025 foreach ( $tables as $alias => $table ) {
2026 if ( is_numeric( $alias ) ) {
2029 $retval[] = $this->tableNameWithAlias( $table, $alias );
2036 * Get an aliased field name
2037 * e.g. fieldName AS newFieldName
2039 * @param string $name Field name
2040 * @param string|bool $alias Alias (optional)
2041 * @return string SQL name for aliased field. Will not alias a field to its own name
2043 public function fieldNameWithAlias( $name, $alias = false ) {
2044 if ( !$alias ||
(string)$alias === (string)$name ) {
2047 return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
2052 * Gets an array of aliased field names
2054 * @param array $fields [ [alias] => field ]
2055 * @return string[] See fieldNameWithAlias()
2057 public function fieldNamesWithAlias( $fields ) {
2059 foreach ( $fields as $alias => $field ) {
2060 if ( is_numeric( $alias ) ) {
2063 $retval[] = $this->fieldNameWithAlias( $field, $alias );
2070 * Get the aliased table name clause for a FROM clause
2071 * which might have a JOIN and/or USE INDEX or IGNORE INDEX clause
2073 * @param array $tables ( [alias] => table )
2074 * @param array $use_index Same as for select()
2075 * @param array $ignore_index Same as for select()
2076 * @param array $join_conds Same as for select()
2079 protected function tableNamesWithIndexClauseOrJOIN(
2080 $tables, $use_index = [], $ignore_index = [], $join_conds = []
2084 $use_index = (array)$use_index;
2085 $ignore_index = (array)$ignore_index;
2086 $join_conds = (array)$join_conds;
2088 foreach ( $tables as $alias => $table ) {
2089 if ( !is_string( $alias ) ) {
2090 // No alias? Set it equal to the table name
2093 // Is there a JOIN clause for this table?
2094 if ( isset( $join_conds[$alias] ) ) {
2095 list( $joinType, $conds ) = $join_conds[$alias];
2096 $tableClause = $joinType;
2097 $tableClause .= ' ' . $this->tableNameWithAlias( $table, $alias );
2098 if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
2099 $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
2101 $tableClause .= ' ' . $use;
2104 if ( isset( $ignore_index[$alias] ) ) { // has IGNORE INDEX?
2105 $ignore = $this->ignoreIndexClause( implode( ',', (array)$ignore_index[$alias] ) );
2106 if ( $ignore != '' ) {
2107 $tableClause .= ' ' . $ignore;
2110 $on = $this->makeList( (array)$conds, LIST_AND
);
2112 $tableClause .= ' ON (' . $on . ')';
2115 $retJOIN[] = $tableClause;
2116 } elseif ( isset( $use_index[$alias] ) ) {
2117 // Is there an INDEX clause for this table?
2118 $tableClause = $this->tableNameWithAlias( $table, $alias );
2119 $tableClause .= ' ' . $this->useIndexClause(
2120 implode( ',', (array)$use_index[$alias] )
2123 $ret[] = $tableClause;
2124 } elseif ( isset( $ignore_index[$alias] ) ) {
2125 // Is there an INDEX clause for this table?
2126 $tableClause = $this->tableNameWithAlias( $table, $alias );
2127 $tableClause .= ' ' . $this->ignoreIndexClause(
2128 implode( ',', (array)$ignore_index[$alias] )
2131 $ret[] = $tableClause;
2133 $tableClause = $this->tableNameWithAlias( $table, $alias );
2135 $ret[] = $tableClause;
2139 // We can't separate explicit JOIN clauses with ',', use ' ' for those
2140 $implicitJoins = !empty( $ret ) ?
implode( ',', $ret ) : "";
2141 $explicitJoins = !empty( $retJOIN ) ?
implode( ' ', $retJOIN ) : "";
2143 // Compile our final table clause
2144 return implode( ' ', [ $implicitJoins, $explicitJoins ] );
2148 * Get the name of an index in a given table.
2150 * @param string $index
2153 protected function indexName( $index ) {
2154 // Backwards-compatibility hack
2156 'ar_usertext_timestamp' => 'usertext_timestamp',
2157 'un_user_id' => 'user_id',
2158 'un_user_ip' => 'user_ip',
2161 if ( isset( $renamed[$index] ) ) {
2162 return $renamed[$index];
2168 public function addQuotes( $s ) {
2169 if ( $s instanceof Blob
) {
2172 if ( $s === null ) {
2175 # This will also quote numeric values. This should be harmless,
2176 # and protects against weird problems that occur when they really
2177 # _are_ strings such as article titles and string->number->string
2178 # conversion is not 1:1.
2179 return "'" . $this->strencode( $s ) . "'";
2184 * Quotes an identifier using `backticks` or "double quotes" depending on the database type.
2185 * MySQL uses `backticks` while basically everything else uses double quotes.
2186 * Since MySQL is the odd one out here the double quotes are our generic
2187 * and we implement backticks in DatabaseMysql.
2192 public function addIdentifierQuotes( $s ) {
2193 return '"' . str_replace( '"', '""', $s ) . '"';
2197 * Returns if the given identifier looks quoted or not according to
2198 * the database convention for quoting identifiers .
2200 * @note Do not use this to determine if untrusted input is safe.
2201 * A malicious user can trick this function.
2202 * @param string $name
2205 public function isQuotedIdentifier( $name ) {
2206 return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
2213 protected function escapeLikeInternal( $s ) {
2214 return addcslashes( $s, '\%_' );
2217 public function buildLike() {
2218 $params = func_get_args();
2220 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
2221 $params = $params[0];
2226 foreach ( $params as $value ) {
2227 if ( $value instanceof LikeMatch
) {
2228 $s .= $value->toString();
2230 $s .= $this->escapeLikeInternal( $value );
2234 return " LIKE {$this->addQuotes( $s )} ";
2237 public function anyChar() {
2238 return new LikeMatch( '_' );
2241 public function anyString() {
2242 return new LikeMatch( '%' );
2245 public function nextSequenceValue( $seqName ) {
2250 * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
2251 * is only needed because a) MySQL must be as efficient as possible due to
2252 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2253 * which index to pick. Anyway, other databases might have different
2254 * indexes on a given table. So don't bother overriding this unless you're
2256 * @param string $index
2259 public function useIndexClause( $index ) {
2264 * IGNORE INDEX clause. Unlikely to be useful for anything but MySQL. This
2265 * is only needed because a) MySQL must be as efficient as possible due to
2266 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2267 * which index to pick. Anyway, other databases might have different
2268 * indexes on a given table. So don't bother overriding this unless you're
2270 * @param string $index
2273 public function ignoreIndexClause( $index ) {
2277 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__
) {
2278 $quotedTable = $this->tableName( $table );
2280 if ( count( $rows ) == 0 ) {
2285 if ( !is_array( reset( $rows ) ) ) {
2289 // @FXIME: this is not atomic, but a trx would break affectedRows()
2290 foreach ( $rows as $row ) {
2291 # Delete rows which collide
2292 if ( $uniqueIndexes ) {
2293 $sql = "DELETE FROM $quotedTable WHERE ";
2295 foreach ( $uniqueIndexes as $index ) {
2302 if ( is_array( $index ) ) {
2304 foreach ( $index as $col ) {
2310 $sql .= $col . '=' . $this->addQuotes( $row[$col] );
2313 $sql .= $index . '=' . $this->addQuotes( $row[$index] );
2317 $this->query( $sql, $fname );
2320 # Now insert the row
2321 $this->insert( $table, $row, $fname );
2326 * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
2329 * @param string $table Table name
2330 * @param array|string $rows Row(s) to insert
2331 * @param string $fname Caller function name
2333 * @return ResultWrapper
2335 protected function nativeReplace( $table, $rows, $fname ) {
2336 $table = $this->tableName( $table );
2339 if ( !is_array( reset( $rows ) ) ) {
2343 $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
2346 foreach ( $rows as $row ) {
2353 $sql .= '(' . $this->makeList( $row ) . ')';
2356 return $this->query( $sql, $fname );
2359 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
2362 if ( !count( $rows ) ) {
2363 return true; // nothing to do
2366 if ( !is_array( reset( $rows ) ) ) {
2370 if ( count( $uniqueIndexes ) ) {
2371 $clauses = []; // list WHERE clauses that each identify a single row
2372 foreach ( $rows as $row ) {
2373 foreach ( $uniqueIndexes as $index ) {
2374 $index = is_array( $index ) ?
$index : [ $index ]; // columns
2375 $rowKey = []; // unique key to this row
2376 foreach ( $index as $column ) {
2377 $rowKey[$column] = $row[$column];
2379 $clauses[] = $this->makeList( $rowKey, LIST_AND
);
2382 $where = [ $this->makeList( $clauses, LIST_OR
) ];
2387 $useTrx = !$this->mTrxLevel
;
2389 $this->begin( $fname, self
::TRANSACTION_INTERNAL
);
2392 # Update any existing conflicting row(s)
2393 if ( $where !== false ) {
2394 $ok = $this->update( $table, $set, $where, $fname );
2398 # Now insert any non-conflicting row(s)
2399 $ok = $this->insert( $table, $rows, $fname, [ 'IGNORE' ] ) && $ok;
2400 } catch ( Exception
$e ) {
2402 $this->rollback( $fname, self
::FLUSHING_INTERNAL
);
2407 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
2413 public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
2417 throw new DBUnexpectedError( $this, __METHOD__
. ' called with empty $conds' );
2420 $delTable = $this->tableName( $delTable );
2421 $joinTable = $this->tableName( $joinTable );
2422 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
2423 if ( $conds != '*' ) {
2424 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
2428 $this->query( $sql, $fname );
2432 * Returns the size of a text field, or -1 for "unlimited"
2434 * @param string $table
2435 * @param string $field
2438 public function textFieldSize( $table, $field ) {
2439 $table = $this->tableName( $table );
2440 $sql = "SHOW COLUMNS FROM $table LIKE \"$field\";";
2441 $res = $this->query( $sql, __METHOD__
);
2442 $row = $this->fetchObject( $res );
2446 if ( preg_match( '/\((.*)\)/', $row->Type
, $m ) ) {
2456 * A string to insert into queries to show that they're low-priority, like
2457 * MySQL's LOW_PRIORITY. If no such feature exists, return an empty
2458 * string and nothing bad should happen.
2460 * @return string Returns the text of the low priority option if it is
2461 * supported, or a blank string otherwise
2463 public function lowPriorityOption() {
2467 public function delete( $table, $conds, $fname = __METHOD__
) {
2469 throw new DBUnexpectedError( $this, __METHOD__
. ' called with no conditions' );
2472 $table = $this->tableName( $table );
2473 $sql = "DELETE FROM $table";
2475 if ( $conds != '*' ) {
2476 if ( is_array( $conds ) ) {
2477 $conds = $this->makeList( $conds, LIST_AND
);
2479 $sql .= ' WHERE ' . $conds;
2482 return $this->query( $sql, $fname );
2485 public function insertSelect(
2486 $destTable, $srcTable, $varMap, $conds,
2487 $fname = __METHOD__
, $insertOptions = [], $selectOptions = []
2489 if ( $this->cliMode
) {
2490 // For massive migrations with downtime, we don't want to select everything
2491 // into memory and OOM, so do all this native on the server side if possible.
2492 return $this->nativeInsertSelect(
2503 // For web requests, do a locking SELECT and then INSERT. This puts the SELECT burden
2504 // on only the master (without needing row-based-replication). It also makes it easy to
2505 // know how big the INSERT is going to be.
2507 foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) {
2508 $fields[] = $this->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn );
2510 $selectOptions[] = 'FOR UPDATE';
2511 $res = $this->select( $srcTable, implode( ',', $fields ), $conds, $fname, $selectOptions );
2517 foreach ( $res as $row ) {
2518 $rows[] = (array)$row;
2521 return $this->insert( $destTable, $rows, $fname, $insertOptions );
2524 public function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
2525 $fname = __METHOD__
,
2526 $insertOptions = [], $selectOptions = []
2528 $destTable = $this->tableName( $destTable );
2530 if ( !is_array( $insertOptions ) ) {
2531 $insertOptions = [ $insertOptions ];
2534 $insertOptions = $this->makeInsertOptions( $insertOptions );
2536 if ( !is_array( $selectOptions ) ) {
2537 $selectOptions = [ $selectOptions ];
2540 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) = $this->makeSelectOptions(
2543 if ( is_array( $srcTable ) ) {
2544 $srcTable = implode( ',', array_map( [ &$this, 'tableName' ], $srcTable ) );
2546 $srcTable = $this->tableName( $srcTable );
2549 $sql = "INSERT $insertOptions INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
2550 " SELECT $startOpts " . implode( ',', $varMap ) .
2551 " FROM $srcTable $useIndex $ignoreIndex ";
2553 if ( $conds != '*' ) {
2554 if ( is_array( $conds ) ) {
2555 $conds = $this->makeList( $conds, LIST_AND
);
2557 $sql .= " WHERE $conds";
2560 $sql .= " $tailOpts";
2562 return $this->query( $sql, $fname );
2566 * Construct a LIMIT query with optional offset. This is used for query
2567 * pages. The SQL should be adjusted so that only the first $limit rows
2568 * are returned. If $offset is provided as well, then the first $offset
2569 * rows should be discarded, and the next $limit rows should be returned.
2570 * If the result of the query is not ordered, then the rows to be returned
2571 * are theoretically arbitrary.
2573 * $sql is expected to be a SELECT, if that makes a difference.
2575 * The version provided by default works in MySQL and SQLite. It will very
2576 * likely need to be overridden for most other DBMSes.
2578 * @param string $sql SQL query we will append the limit too
2579 * @param int $limit The SQL limit
2580 * @param int|bool $offset The SQL offset (default false)
2581 * @throws DBUnexpectedError
2584 public function limitResult( $sql, $limit, $offset = false ) {
2585 if ( !is_numeric( $limit ) ) {
2586 throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
2589 return "$sql LIMIT "
2590 . ( ( is_numeric( $offset ) && $offset != 0 ) ?
"{$offset}," : "" )
2594 public function unionSupportsOrderAndLimit() {
2595 return true; // True for almost every DB supported
2598 public function unionQueries( $sqls, $all ) {
2599 $glue = $all ?
') UNION ALL (' : ') UNION (';
2601 return '(' . implode( $glue, $sqls ) . ')';
2604 public function conditional( $cond, $trueVal, $falseVal ) {
2605 if ( is_array( $cond ) ) {
2606 $cond = $this->makeList( $cond, LIST_AND
);
2609 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
2612 public function strreplace( $orig, $old, $new ) {
2613 return "REPLACE({$orig}, {$old}, {$new})";
2616 public function getServerUptime() {
2620 public function wasDeadlock() {
2624 public function wasLockTimeout() {
2628 public function wasErrorReissuable() {
2632 public function wasReadOnlyError() {
2637 * Determines if the given query error was a connection drop
2640 * @param integer|string $errno
2643 public function wasConnectionError( $errno ) {
2648 * Perform a deadlock-prone transaction.
2650 * This function invokes a callback function to perform a set of write
2651 * queries. If a deadlock occurs during the processing, the transaction
2652 * will be rolled back and the callback function will be called again.
2654 * Avoid using this method outside of Job or Maintenance classes.
2657 * $dbw->deadlockLoop( callback, ... );
2659 * Extra arguments are passed through to the specified callback function.
2660 * This method requires that no transactions are already active to avoid
2661 * causing premature commits or exceptions.
2663 * Returns whatever the callback function returned on its successful,
2664 * iteration, or false on error, for example if the retry limit was
2668 * @throws DBUnexpectedError
2671 public function deadlockLoop() {
2672 $args = func_get_args();
2673 $function = array_shift( $args );
2674 $tries = self
::DEADLOCK_TRIES
;
2676 $this->begin( __METHOD__
);
2679 /** @var Exception $e */
2683 $retVal = call_user_func_array( $function, $args );
2685 } catch ( DBQueryError
$e ) {
2686 if ( $this->wasDeadlock() ) {
2687 // Retry after a randomized delay
2688 usleep( mt_rand( self
::DEADLOCK_DELAY_MIN
, self
::DEADLOCK_DELAY_MAX
) );
2690 // Throw the error back up
2694 } while ( --$tries > 0 );
2696 if ( $tries <= 0 ) {
2697 // Too many deadlocks; give up
2698 $this->rollback( __METHOD__
);
2701 $this->commit( __METHOD__
);
2707 public function masterPosWait( DBMasterPos
$pos, $timeout ) {
2708 # Real waits are implemented in the subclass.
2712 public function getSlavePos() {
2717 public function getMasterPos() {
2722 public function serverIsReadOnly() {
2726 final public function onTransactionResolution( callable
$callback ) {
2727 if ( !$this->mTrxLevel
) {
2728 throw new DBUnexpectedError( $this, "No transaction is active." );
2730 $this->mTrxEndCallbacks
[] = [ $callback, wfGetCaller() ];
2733 final public function onTransactionIdle( callable
$callback ) {
2734 $this->mTrxIdleCallbacks
[] = [ $callback, wfGetCaller() ];
2735 if ( !$this->mTrxLevel
) {
2736 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_IDLE
);
2740 final public function onTransactionPreCommitOrIdle( callable
$callback ) {
2741 if ( $this->mTrxLevel
) {
2742 $this->mTrxPreCommitCallbacks
[] = [ $callback, wfGetCaller() ];
2744 // If no transaction is active, then make one for this callback
2745 $this->startAtomic( __METHOD__
);
2747 call_user_func( $callback );
2748 $this->endAtomic( __METHOD__
);
2749 } catch ( Exception
$e ) {
2750 $this->rollback( __METHOD__
, self
::FLUSHING_INTERNAL
);
2756 final public function setTransactionListener( $name, callable
$callback = null ) {
2758 $this->mTrxRecurringCallbacks
[$name] = [ $callback, wfGetCaller() ];
2760 unset( $this->mTrxRecurringCallbacks
[$name] );
2765 * Whether to disable running of post-COMMIT/ROLLBACK callbacks
2767 * This method should not be used outside of Database/LoadBalancer
2769 * @param bool $suppress
2772 final public function setTrxEndCallbackSuppression( $suppress ) {
2773 $this->mTrxEndCallbacksSuppressed
= $suppress;
2777 * Actually run and consume any "on transaction idle/resolution" callbacks.
2779 * This method should not be used outside of Database/LoadBalancer
2781 * @param integer $trigger IDatabase::TRIGGER_* constant
2785 public function runOnTransactionIdleCallbacks( $trigger ) {
2786 if ( $this->mTrxEndCallbacksSuppressed
) {
2790 $autoTrx = $this->getFlag( DBO_TRX
); // automatic begin() enabled?
2791 /** @var Exception $e */
2792 $e = null; // first exception
2793 do { // callbacks may add callbacks :)
2794 $callbacks = array_merge(
2795 $this->mTrxIdleCallbacks
,
2796 $this->mTrxEndCallbacks
// include "transaction resolution" callbacks
2798 $this->mTrxIdleCallbacks
= []; // consumed (and recursion guard)
2799 $this->mTrxEndCallbacks
= []; // consumed (recursion guard)
2800 foreach ( $callbacks as $callback ) {
2802 list( $phpCallback ) = $callback;
2803 $this->clearFlag( DBO_TRX
); // make each query its own transaction
2804 call_user_func_array( $phpCallback, [ $trigger ] );
2806 $this->setFlag( DBO_TRX
); // restore automatic begin()
2808 $this->clearFlag( DBO_TRX
); // restore auto-commit
2810 } catch ( Exception
$ex ) {
2811 call_user_func( $this->errorLogger
, $ex );
2813 // Some callbacks may use startAtomic/endAtomic, so make sure
2814 // their transactions are ended so other callbacks don't fail
2815 if ( $this->trxLevel() ) {
2816 $this->rollback( __METHOD__
, self
::FLUSHING_INTERNAL
);
2820 } while ( count( $this->mTrxIdleCallbacks
) );
2822 if ( $e instanceof Exception
) {
2823 throw $e; // re-throw any first exception
2828 * Actually run and consume any "on transaction pre-commit" callbacks.
2830 * This method should not be used outside of Database/LoadBalancer
2835 public function runOnTransactionPreCommitCallbacks() {
2836 $e = null; // first exception
2837 do { // callbacks may add callbacks :)
2838 $callbacks = $this->mTrxPreCommitCallbacks
;
2839 $this->mTrxPreCommitCallbacks
= []; // consumed (and recursion guard)
2840 foreach ( $callbacks as $callback ) {
2842 list( $phpCallback ) = $callback;
2843 call_user_func( $phpCallback );
2844 } catch ( Exception
$ex ) {
2845 call_user_func( $this->errorLogger
, $ex );
2849 } while ( count( $this->mTrxPreCommitCallbacks
) );
2851 if ( $e instanceof Exception
) {
2852 throw $e; // re-throw any first exception
2857 * Actually run any "transaction listener" callbacks.
2859 * This method should not be used outside of Database/LoadBalancer
2861 * @param integer $trigger IDatabase::TRIGGER_* constant
2865 public function runTransactionListenerCallbacks( $trigger ) {
2866 if ( $this->mTrxEndCallbacksSuppressed
) {
2870 /** @var Exception $e */
2871 $e = null; // first exception
2873 foreach ( $this->mTrxRecurringCallbacks
as $callback ) {
2875 list( $phpCallback ) = $callback;
2876 $phpCallback( $trigger, $this );
2877 } catch ( Exception
$ex ) {
2878 call_user_func( $this->errorLogger
, $ex );
2883 if ( $e instanceof Exception
) {
2884 throw $e; // re-throw any first exception
2888 final public function startAtomic( $fname = __METHOD__
) {
2889 if ( !$this->mTrxLevel
) {
2890 $this->begin( $fname, self
::TRANSACTION_INTERNAL
);
2891 // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
2892 // in all changes being in one transaction to keep requests transactional.
2893 if ( !$this->getFlag( DBO_TRX
) ) {
2894 $this->mTrxAutomaticAtomic
= true;
2898 $this->mTrxAtomicLevels
[] = $fname;
2901 final public function endAtomic( $fname = __METHOD__
) {
2902 if ( !$this->mTrxLevel
) {
2903 throw new DBUnexpectedError( $this, "No atomic transaction is open (got $fname)." );
2905 if ( !$this->mTrxAtomicLevels ||
2906 array_pop( $this->mTrxAtomicLevels
) !== $fname
2908 throw new DBUnexpectedError( $this, "Invalid atomic section ended (got $fname)." );
2911 if ( !$this->mTrxAtomicLevels
&& $this->mTrxAutomaticAtomic
) {
2912 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
2916 final public function doAtomicSection( $fname, callable
$callback ) {
2917 $this->startAtomic( $fname );
2919 $res = call_user_func_array( $callback, [ $this, $fname ] );
2920 } catch ( Exception
$e ) {
2921 $this->rollback( $fname, self
::FLUSHING_INTERNAL
);
2924 $this->endAtomic( $fname );
2929 final public function begin( $fname = __METHOD__
, $mode = self
::TRANSACTION_EXPLICIT
) {
2930 // Protect against mismatched atomic section, transaction nesting, and snapshot loss
2931 if ( $this->mTrxLevel
) {
2932 if ( $this->mTrxAtomicLevels
) {
2933 $levels = implode( ', ', $this->mTrxAtomicLevels
);
2934 $msg = "$fname: Got explicit BEGIN while atomic section(s) $levels are open.";
2935 throw new DBUnexpectedError( $this, $msg );
2936 } elseif ( !$this->mTrxAutomatic
) {
2937 $msg = "$fname: Explicit transaction already active (from {$this->mTrxFname}).";
2938 throw new DBUnexpectedError( $this, $msg );
2940 // @TODO: make this an exception at some point
2941 $msg = "$fname: Implicit transaction already active (from {$this->mTrxFname}).";
2942 $this->queryLogger
->error( $msg );
2943 return; // join the main transaction set
2945 } elseif ( $this->getFlag( DBO_TRX
) && $mode !== self
::TRANSACTION_INTERNAL
) {
2946 // @TODO: make this an exception at some point
2947 $msg = "$fname: Implicit transaction expected (DBO_TRX set).";
2948 $this->queryLogger
->error( $msg );
2949 return; // let any writes be in the main transaction
2952 // Avoid fatals if close() was called
2953 $this->assertOpen();
2955 $this->doBegin( $fname );
2956 $this->mTrxTimestamp
= microtime( true );
2957 $this->mTrxFname
= $fname;
2958 $this->mTrxDoneWrites
= false;
2959 $this->mTrxAutomatic
= ( $mode === self
::TRANSACTION_INTERNAL
);
2960 $this->mTrxAutomaticAtomic
= false;
2961 $this->mTrxAtomicLevels
= [];
2962 $this->mTrxShortId
= wfRandomString( 12 );
2963 $this->mTrxWriteDuration
= 0.0;
2964 $this->mTrxWriteQueryCount
= 0;
2965 $this->mTrxWriteAdjDuration
= 0.0;
2966 $this->mTrxWriteAdjQueryCount
= 0;
2967 $this->mTrxWriteCallers
= [];
2968 // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
2969 // Get an estimate of the replica DB lag before then, treating estimate staleness
2970 // as lag itself just to be safe
2971 $status = $this->getApproximateLagStatus();
2972 $this->mTrxReplicaLag
= $status['lag'] +
( microtime( true ) - $status['since'] );
2976 * Issues the BEGIN command to the database server.
2978 * @see DatabaseBase::begin()
2979 * @param string $fname
2981 protected function doBegin( $fname ) {
2982 $this->query( 'BEGIN', $fname );
2983 $this->mTrxLevel
= 1;
2986 final public function commit( $fname = __METHOD__
, $flush = '' ) {
2987 if ( $this->mTrxLevel
&& $this->mTrxAtomicLevels
) {
2988 // There are still atomic sections open. This cannot be ignored
2989 $levels = implode( ', ', $this->mTrxAtomicLevels
);
2990 throw new DBUnexpectedError(
2992 "$fname: Got COMMIT while atomic sections $levels are still open."
2996 if ( $flush === self
::FLUSHING_INTERNAL ||
$flush === self
::FLUSHING_ALL_PEERS
) {
2997 if ( !$this->mTrxLevel
) {
2998 return; // nothing to do
2999 } elseif ( !$this->mTrxAutomatic
) {
3000 throw new DBUnexpectedError(
3002 "$fname: Flushing an explicit transaction, getting out of sync."
3006 if ( !$this->mTrxLevel
) {
3007 $this->queryLogger
->error( "$fname: No transaction to commit, something got out of sync." );
3008 return; // nothing to do
3009 } elseif ( $this->mTrxAutomatic
) {
3010 // @TODO: make this an exception at some point
3011 $msg = "$fname: Explicit commit of implicit transaction.";
3012 $this->queryLogger
->error( $msg );
3013 return; // wait for the main transaction set commit round
3017 // Avoid fatals if close() was called
3018 $this->assertOpen();
3020 $this->runOnTransactionPreCommitCallbacks();
3021 $writeTime = $this->pendingWriteQueryDuration( self
::ESTIMATE_DB_APPLY
);
3022 $this->doCommit( $fname );
3023 if ( $this->mTrxDoneWrites
) {
3024 $this->mDoneWrites
= microtime( true );
3025 $this->trxProfiler
->transactionWritingOut(
3026 $this->mServer
, $this->mDBname
, $this->mTrxShortId
, $writeTime );
3029 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_COMMIT
);
3030 $this->runTransactionListenerCallbacks( self
::TRIGGER_COMMIT
);
3034 * Issues the COMMIT command to the database server.
3036 * @see DatabaseBase::commit()
3037 * @param string $fname
3039 protected function doCommit( $fname ) {
3040 if ( $this->mTrxLevel
) {
3041 $this->query( 'COMMIT', $fname );
3042 $this->mTrxLevel
= 0;
3046 final public function rollback( $fname = __METHOD__
, $flush = '' ) {
3047 if ( $flush === self
::FLUSHING_INTERNAL ||
$flush === self
::FLUSHING_ALL_PEERS
) {
3048 if ( !$this->mTrxLevel
) {
3049 return; // nothing to do
3052 if ( !$this->mTrxLevel
) {
3053 $this->queryLogger
->error(
3054 "$fname: No transaction to rollback, something got out of sync." );
3055 return; // nothing to do
3056 } elseif ( $this->getFlag( DBO_TRX
) ) {
3057 throw new DBUnexpectedError(
3059 "$fname: Expected mass rollback of all peer databases (DBO_TRX set)."
3064 // Avoid fatals if close() was called
3065 $this->assertOpen();
3067 $this->doRollback( $fname );
3068 $this->mTrxAtomicLevels
= [];
3069 if ( $this->mTrxDoneWrites
) {
3070 $this->trxProfiler
->transactionWritingOut(
3071 $this->mServer
, $this->mDBname
, $this->mTrxShortId
);
3074 $this->mTrxIdleCallbacks
= []; // clear
3075 $this->mTrxPreCommitCallbacks
= []; // clear
3076 $this->runOnTransactionIdleCallbacks( self
::TRIGGER_ROLLBACK
);
3077 $this->runTransactionListenerCallbacks( self
::TRIGGER_ROLLBACK
);
3081 * Issues the ROLLBACK command to the database server.
3083 * @see DatabaseBase::rollback()
3084 * @param string $fname
3086 protected function doRollback( $fname ) {
3087 if ( $this->mTrxLevel
) {
3088 # Disconnects cause rollback anyway, so ignore those errors
3089 $ignoreErrors = true;
3090 $this->query( 'ROLLBACK', $fname, $ignoreErrors );
3091 $this->mTrxLevel
= 0;
3095 public function flushSnapshot( $fname = __METHOD__
) {
3096 if ( $this->writesOrCallbacksPending() ||
$this->explicitTrxActive() ) {
3097 // This only flushes transactions to clear snapshots, not to write data
3098 throw new DBUnexpectedError(
3100 "$fname: Cannot COMMIT to clear snapshot because writes are pending."
3104 $this->commit( $fname, self
::FLUSHING_INTERNAL
);
3107 public function explicitTrxActive() {
3108 return $this->mTrxLevel
&& ( $this->mTrxAtomicLevels ||
!$this->mTrxAutomatic
);
3112 * Creates a new table with structure copied from existing table
3113 * Note that unlike most database abstraction functions, this function does not
3114 * automatically append database prefix, because it works at a lower
3115 * abstraction level.
3116 * The table names passed to this function shall not be quoted (this
3117 * function calls addIdentifierQuotes when needed).
3119 * @param string $oldName Name of table whose structure should be copied
3120 * @param string $newName Name of table to be created
3121 * @param bool $temporary Whether the new table should be temporary
3122 * @param string $fname Calling function name
3123 * @throws RuntimeException
3124 * @return bool True if operation was successful
3126 public function duplicateTableStructure( $oldName, $newName, $temporary = false,
3129 throw new RuntimeException( __METHOD__
. ' is not implemented in descendant class' );
3132 function listTables( $prefix = null, $fname = __METHOD__
) {
3133 throw new RuntimeException( __METHOD__
. ' is not implemented in descendant class' );
3137 * Reset the views process cache set by listViews()
3140 final public function clearViewsCache() {
3141 $this->allViews
= null;
3145 * Lists all the VIEWs in the database
3147 * For caching purposes the list of all views should be stored in
3148 * $this->allViews. The process cache can be cleared with clearViewsCache()
3150 * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
3151 * @param string $fname Name of calling function
3152 * @throws RuntimeException
3156 public function listViews( $prefix = null, $fname = __METHOD__
) {
3157 throw new RuntimeException( __METHOD__
. ' is not implemented in descendant class' );
3161 * Differentiates between a TABLE and a VIEW
3163 * @param string $name Name of the database-structure to test.
3164 * @throws RuntimeException
3168 public function isView( $name ) {
3169 throw new RuntimeException( __METHOD__
. ' is not implemented in descendant class' );
3172 public function timestamp( $ts = 0 ) {
3173 return wfTimestamp( TS_MW
, $ts );
3176 public function timestampOrNull( $ts = null ) {
3177 if ( is_null( $ts ) ) {
3180 return $this->timestamp( $ts );
3185 * Take the result from a query, and wrap it in a ResultWrapper if
3186 * necessary. Boolean values are passed through as is, to indicate success
3187 * of write queries or failure.
3189 * Once upon a time, DatabaseBase::query() returned a bare MySQL result
3190 * resource, and it was necessary to call this function to convert it to
3191 * a wrapper. Nowadays, raw database objects are never exposed to external
3192 * callers, so this is unnecessary in external code.
3194 * @param bool|ResultWrapper|resource|object $result
3195 * @return bool|ResultWrapper
3197 protected function resultObject( $result ) {
3200 } elseif ( $result instanceof ResultWrapper
) {
3202 } elseif ( $result === true ) {
3203 // Successful write query
3206 return new ResultWrapper( $this, $result );
3210 public function ping( &$rtt = null ) {
3211 // Avoid hitting the server if it was hit recently
3212 if ( $this->isOpen() && ( microtime( true ) - $this->lastPing
) < self
::PING_TTL
) {
3213 if ( !func_num_args() ||
$this->mRTTEstimate
> 0 ) {
3214 $rtt = $this->mRTTEstimate
;
3215 return true; // don't care about $rtt
3219 // This will reconnect if possible or return false if not
3220 $this->clearFlag( DBO_TRX
, self
::REMEMBER_PRIOR
);
3221 $ok = ( $this->query( self
::PING_QUERY
, __METHOD__
, true ) !== false );
3222 $this->restoreFlags( self
::RESTORE_PRIOR
);
3225 $rtt = $this->mRTTEstimate
;
3234 protected function reconnect() {
3235 $this->closeConnection();
3236 $this->mOpened
= false;
3237 $this->mConn
= false;
3239 $this->open( $this->mServer
, $this->mUser
, $this->mPassword
, $this->mDBname
);
3240 $this->lastPing
= microtime( true );
3242 } catch ( DBConnectionError
$e ) {
3249 public function getSessionLagStatus() {
3250 return $this->getTransactionLagStatus() ?
: $this->getApproximateLagStatus();
3254 * Get the replica DB lag when the current transaction started
3256 * This is useful when transactions might use snapshot isolation
3257 * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
3258 * is this lag plus transaction duration. If they don't, it is still
3259 * safe to be pessimistic. This returns null if there is no transaction.
3261 * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
3264 public function getTransactionLagStatus() {
3265 return $this->mTrxLevel
3266 ?
[ 'lag' => $this->mTrxReplicaLag
, 'since' => $this->trxTimestamp() ]
3271 * Get a replica DB lag estimate for this server
3273 * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate)
3276 public function getApproximateLagStatus() {
3278 'lag' => $this->getLBInfo( 'replica' ) ?
$this->getLag() : 0,
3279 'since' => microtime( true )
3284 * Merge the result of getSessionLagStatus() for several DBs
3285 * using the most pessimistic values to estimate the lag of
3286 * any data derived from them in combination
3288 * This is information is useful for caching modules
3290 * @see WANObjectCache::set()
3291 * @see WANObjectCache::getWithSetCallback()
3293 * @param IDatabase $db1
3294 * @param IDatabase ...
3295 * @return array Map of values:
3296 * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped)
3297 * - since: oldest UNIX timestamp of any of the DB lag estimates
3298 * - pending: whether any of the DBs have uncommitted changes
3301 public static function getCacheSetOptions( IDatabase
$db1 ) {
3302 $res = [ 'lag' => 0, 'since' => INF
, 'pending' => false ];
3303 foreach ( func_get_args() as $db ) {
3304 /** @var IDatabase $db */
3305 $status = $db->getSessionLagStatus();
3306 if ( $status['lag'] === false ) {
3307 $res['lag'] = false;
3308 } elseif ( $res['lag'] !== false ) {
3309 $res['lag'] = max( $res['lag'], $status['lag'] );
3311 $res['since'] = min( $res['since'], $status['since'] );
3312 $res['pending'] = $res['pending'] ?
: $db->writesPending();
3318 public function getLag() {
3322 function maxListLen() {
3326 public function encodeBlob( $b ) {
3330 public function decodeBlob( $b ) {
3331 if ( $b instanceof Blob
) {
3337 public function setSessionOptions( array $options ) {
3341 * Read and execute SQL commands from a file.
3343 * Returns true on success, error string or exception on failure (depending
3344 * on object's error ignore settings).
3346 * @param string $filename File name to open
3347 * @param bool|callable $lineCallback Optional function called before reading each line
3348 * @param bool|callable $resultCallback Optional function called for each MySQL result
3349 * @param bool|string $fname Calling function name or false if name should be
3350 * generated dynamically using $filename
3351 * @param bool|callable $inputCallback Optional function called for each
3352 * complete line sent
3353 * @return bool|string
3356 public function sourceFile(
3357 $filename, $lineCallback = false, $resultCallback = false, $fname = false, $inputCallback = false
3359 MediaWiki\
suppressWarnings();
3360 $fp = fopen( $filename, 'r' );
3361 MediaWiki\restoreWarnings
();
3363 if ( false === $fp ) {
3364 throw new RuntimeException( "Could not open \"{$filename}\".\n" );
3368 $fname = __METHOD__
. "( $filename )";
3372 $error = $this->sourceStream( $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
3373 } catch ( Exception
$e ) {
3384 * Get the full path of a patch file. Originally based on archive()
3385 * from updaters.inc. Keep in mind this always returns a patch, as
3386 * it fails back to MySQL if no DB-specific patch can be found
3388 * @param string $patch The name of the patch, like patch-something.sql
3389 * @return string Full path to patch file
3391 public function patchPath( $patch ) {
3394 $dbType = $this->getType();
3395 if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
3396 return "$IP/maintenance/$dbType/archives/$patch";
3398 return "$IP/maintenance/archives/$patch";
3402 public function setSchemaVars( $vars ) {
3403 $this->mSchemaVars
= $vars;
3407 * Read and execute commands from an open file handle.
3409 * Returns true on success, error string or exception on failure (depending
3410 * on object's error ignore settings).
3412 * @param resource $fp File handle
3413 * @param bool|callable $lineCallback Optional function called before reading each query
3414 * @param bool|callable $resultCallback Optional function called for each MySQL result
3415 * @param string $fname Calling function name
3416 * @param bool|callable $inputCallback Optional function called for each complete query sent
3417 * @return bool|string
3419 public function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
3420 $fname = __METHOD__
, $inputCallback = false
3424 while ( !feof( $fp ) ) {
3425 if ( $lineCallback ) {
3426 call_user_func( $lineCallback );
3429 $line = trim( fgets( $fp ) );
3431 if ( $line == '' ) {
3435 if ( '-' == $line[0] && '-' == $line[1] ) {
3443 $done = $this->streamStatementEnd( $cmd, $line );
3447 if ( $done ||
feof( $fp ) ) {
3448 $cmd = $this->replaceVars( $cmd );
3450 if ( ( $inputCallback && call_user_func( $inputCallback, $cmd ) ) ||
!$inputCallback ) {
3451 $res = $this->query( $cmd, $fname );
3453 if ( $resultCallback ) {
3454 call_user_func( $resultCallback, $res, $this );
3457 if ( false === $res ) {
3458 $err = $this->lastError();
3460 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
3471 * Called by sourceStream() to check if we've reached a statement end
3473 * @param string $sql SQL assembled so far
3474 * @param string $newLine New line about to be added to $sql
3475 * @return bool Whether $newLine contains end of the statement
3477 public function streamStatementEnd( &$sql, &$newLine ) {
3478 if ( $this->delimiter
) {
3480 $newLine = preg_replace( '/' . preg_quote( $this->delimiter
, '/' ) . '$/', '', $newLine );
3481 if ( $newLine != $prev ) {
3490 * Database independent variable replacement. Replaces a set of variables
3491 * in an SQL statement with their contents as given by $this->getSchemaVars().
3493 * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
3495 * - '{$var}' should be used for text and is passed through the database's
3497 * - `{$var}` should be used for identifiers (e.g. table and database names).
3498 * It is passed through the database's addIdentifierQuotes method which
3499 * can be overridden if the database uses something other than backticks.
3500 * - / *_* / or / *$wgDBprefix* / passes the name that follows through the
3501 * database's tableName method.
3502 * - / *i* / passes the name that follows through the database's indexName method.
3503 * - In all other cases, / *$var* / is left unencoded. Except for table options,
3504 * its use should be avoided. In 1.24 and older, string encoding was applied.
3506 * @param string $ins SQL statement to replace variables in
3507 * @return string The new SQL statement with variables replaced
3509 protected function replaceVars( $ins ) {
3510 $vars = $this->getSchemaVars();
3511 return preg_replace_callback(
3513 /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
3514 \'\{\$ (\w+) }\' | # 3. addQuotes
3515 `\{\$ (\w+) }` | # 4. addIdentifierQuotes
3516 /\*\$ (\w+) \*/ # 5. leave unencoded
3518 function ( $m ) use ( $vars ) {
3519 // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
3520 // check for both nonexistent keys *and* the empty string.
3521 if ( isset( $m[1] ) && $m[1] !== '' ) {
3522 if ( $m[1] === 'i' ) {
3523 return $this->indexName( $m[2] );
3525 return $this->tableName( $m[2] );
3527 } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
3528 return $this->addQuotes( $vars[$m[3]] );
3529 } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
3530 return $this->addIdentifierQuotes( $vars[$m[4]] );
3531 } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
3532 return $vars[$m[5]];
3542 * Get schema variables. If none have been set via setSchemaVars(), then
3543 * use some defaults from the current object.
3547 protected function getSchemaVars() {
3548 if ( $this->mSchemaVars
) {
3549 return $this->mSchemaVars
;
3551 return $this->getDefaultSchemaVars();
3556 * Get schema variables to use if none have been set via setSchemaVars().
3558 * Override this in derived classes to provide variables for tables.sql
3559 * and SQL patch files.
3563 protected function getDefaultSchemaVars() {
3567 public function lockIsFree( $lockName, $method ) {
3571 public function lock( $lockName, $method, $timeout = 5 ) {
3572 $this->mNamedLocksHeld
[$lockName] = 1;
3577 public function unlock( $lockName, $method ) {
3578 unset( $this->mNamedLocksHeld
[$lockName] );
3583 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
3584 if ( $this->writesOrCallbacksPending() ) {
3585 // This only flushes transactions to clear snapshots, not to write data
3586 throw new DBUnexpectedError(
3588 "$fname: Cannot COMMIT to clear snapshot because writes are pending."
3592 if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
3596 $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
3597 if ( $this->trxLevel() ) {
3598 // There is a good chance an exception was thrown, causing any early return
3599 // from the caller. Let any error handler get a chance to issue rollback().
3600 // If there isn't one, let the error bubble up and trigger server-side rollback.
3601 $this->onTransactionResolution( function () use ( $lockKey, $fname ) {
3602 $this->unlock( $lockKey, $fname );
3605 $this->unlock( $lockKey, $fname );
3609 $this->commit( __METHOD__
, self
::FLUSHING_INTERNAL
);
3614 public function namedLocksEnqueue() {
3619 * Lock specific tables
3621 * @param array $read Array of tables to lock for read access
3622 * @param array $write Array of tables to lock for write access
3623 * @param string $method Name of caller
3624 * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY
3627 public function lockTables( $read, $write, $method, $lowPriority = true ) {
3632 * Unlock specific tables
3634 * @param string $method The caller
3637 public function unlockTables( $method ) {
3643 * @param string $tableName
3644 * @param string $fName
3645 * @return bool|ResultWrapper
3648 public function dropTable( $tableName, $fName = __METHOD__
) {
3649 if ( !$this->tableExists( $tableName, $fName ) ) {
3652 $sql = "DROP TABLE " . $this->tableName( $tableName );
3653 if ( $this->cascadingDeletes() ) {
3657 return $this->query( $sql, $fName );
3661 * Get search engine class. All subclasses of this need to implement this
3662 * if they wish to use searching.
3666 public function getSearchEngine() {
3667 return 'SearchEngineDummy';
3670 public function getInfinity() {
3674 public function encodeExpiry( $expiry ) {
3675 return ( $expiry == '' ||
$expiry == 'infinity' ||
$expiry == $this->getInfinity() )
3676 ?
$this->getInfinity()
3677 : $this->timestamp( $expiry );
3680 public function decodeExpiry( $expiry, $format = TS_MW
) {
3681 return ( $expiry == '' ||
$expiry == 'infinity' ||
$expiry == $this->getInfinity() )
3683 : wfTimestamp( $format, $expiry );
3686 public function setBigSelects( $value = true ) {
3690 public function isReadOnly() {
3691 return ( $this->getReadOnlyReason() !== false );
3695 * @return string|bool Reason this DB is read-only or false if it is not
3697 protected function getReadOnlyReason() {
3698 $reason = $this->getLBInfo( 'readOnlyReason' );
3700 return is_string( $reason ) ?
$reason : false;
3707 public function __toString() {
3708 return (string)$this->mConn
;
3712 * Run a few simple sanity checks
3714 public function __destruct() {
3715 if ( $this->mTrxLevel
&& $this->mTrxDoneWrites
) {
3716 trigger_error( "Uncommitted DB writes (transaction from {$this->mTrxFname})." );
3718 $danglingCallbacks = array_merge(
3719 $this->mTrxIdleCallbacks
,
3720 $this->mTrxPreCommitCallbacks
,
3721 $this->mTrxEndCallbacks
3723 if ( $danglingCallbacks ) {
3725 foreach ( $danglingCallbacks as $callbackInfo ) {
3726 $callers[] = $callbackInfo[1];
3728 $callers = implode( ', ', $callers );
3729 trigger_error( "DB transaction callbacks still pending (from $callers)." );
3737 abstract class Database
extends DatabaseBase
{
3738 // B/C until nothing type hints for DatabaseBase
3739 // @TODO: finish renaming DatabaseBase => Database